package com.emep.changzhi.analyse.http.basics;


import com.emep.changzhi.analyse.entity.basics.EPA;
import com.emep.changzhi.analyse.entity.basics.SysDepartmentInfo;
import com.emep.changzhi.analyse.entity.basics.SysMonitorSiteInfo;
import com.emep.changzhi.analyse.model.ResultBean;
import com.emep.changzhi.analyse.service.basics.SysMonitorSiteInfoService;
import com.emep.changzhi.analyse.utils.Constant;
import com.emep.changzhi.analyse.utils.DataValidator;
import com.emep.changzhi.analyse.utils.MapToObject;
import com.emep.changzhi.analyse.utils.PageChunk;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("citymonitor")
@Api(value="监控站点信息controller",tags={"监控站点信息"})
public class SysMonitorSiteInfoController {

    @Autowired
    private SysMonitorSiteInfoService service;

    MapToObject<SysMonitorSiteInfo> mapToObject=new MapToObject<>();
    /**
     * 新增监控站点信息
     * @param payload
     * @return
     */
    @ApiOperation(value="新增监控站点信息", notes="新增监控站点信息")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "monitorSiteName", value = "站点名字", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "areaCode", value = "行政区划", required = true, dataType = "String",paramType="query")
    })
    @PostMapping(value = "add")
    public ResultBean<SysMonitorSiteInfo> add(@RequestBody Map<String, Object> payload){
        ResultBean result = DataValidator.validateMapData(payload, "citymonitor");
        if(result.getCode() == Constant.RESULT_CODE_SUCCESS){
            Map<String, Object> map = (Map<String, Object>) payload.get("citymonitor");
          SysMonitorSiteInfo entity=  new SysMonitorSiteInfo() ;
            mapToObject.convert(entity,map);
            boolean b= mapToObject.convertEntity(entity,map);
            if (b){
                boolean flag= service.save(entity);
                if(flag){
                    return result.boolAdd("新增成功");
                }else {
                    return result.error("该名称已存在");
                }
            }else {
                return result.error("参数失败");
            }



        }
        return  result;
    }

    /**
     *
     * @param payload
     * @return
     */
    @ApiOperation(value="修改监控站点信息", notes="新增监控站点信息")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "monitorSiteName", value = "站点名字", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "areaCode", value = "行政区划", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "id", value = "监控站点ID", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "isDelete", value = "删除状态", required = true, dataType = "String",paramType="query"),
    })
    @PostMapping(value = "update")
    public ResultBean<SysMonitorSiteInfo> update(@RequestBody Map<String, Object> payload){
        ResultBean result = DataValidator.validateMapData(payload, "citymonitor");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS) {
            Map<String, Object> map = (Map<String, Object>) payload.get("citymonitor");
            if (!map.containsKey("citymonitorId")) {
                return result.error("citymonitorId为必传字段");
            } else {
                String id = map.get("citymonitorId").toString();
                SysMonitorSiteInfo entity = service.findById(id);
                if (entity != null) {
                    boolean b = mapToObject.convertEntity(entity, map);
                    if (entity != null) {
                        if (b) {
                            boolean a = service.update(entity);
                            if (a) {
                                return result.boolAdd("修改成功");
                            } else {
                                return result.boolAdd("该名称已存在");
                            }
                        } else {
                            return result.error("参数错误");
                        }
                    } else {
                        return result.boolAdd("无此员工");
                    }
                }

            }
        }
        return  result;
    }

    /**
     * 根据id 删除监控站点
     * @param payload
     */
    @ApiOperation(value="删除监控站点信息", notes="根据id删除监控站点信息")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "监控站点id", required = true, dataType = "String",paramType="query"),
    })
    @PostMapping(value = "/delete")
    public ResultBean delete(@RequestBody Map<String, Object> payload){
        ResultBean result = DataValidator.validateMapData(payload, "citymonitorId");
        String id=(String)payload.get("citymonitorId");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS){
            boolean b=service.delete(id);
            if (b){
                return result.boolAdd("删除成功");
            }else {
                return result.error("删除失败");
            }
        }
        return result;
    }

    /**
     * 查询所有监控站点信息
     * @return
     */
    @ApiOperation(value="查询所有监控站点信息", notes="无参数查询监控站点信息")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @PostMapping(value = "list")
    public ResultBean<List<SysMonitorSiteInfo>> queryRegion(@RequestBody Map<String, Object> payload){
        ResultBean result = DataValidator.validateMapData(payload);
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS) {
            List<SysMonitorSiteInfo> industryList =service.findAll();
            if(industryList != null){
                return result.boolAdd(industryList);
            }else {
                return result.error("未查到站点列表");
            }
        }
        return result;
    }

    /**
     * 通过id 根据id查询监控站点信息
     * @param payload
     * @return
     */
    @ApiOperation(value="根据id查询监控站点信息", notes="根据id查询监控站点信息")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @PostMapping(value = "info")
    public ResultBean<SysMonitorSiteInfo> findAreaById(@RequestBody Map<String, Object> payload){
        ResultBean result = DataValidator.validateMapData(payload, "citymonitorId");
        String id=(String)payload.get("citymonitorId");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS) {
            SysMonitorSiteInfo info=service.findById(id);
            if (info != null) {
                return result.boolAdd(info);
            } else {
                return result.error("找不到监控站点");
            }
        }
        return result;
    }

    /**
     * 通过areaCode 根据areaCode查询监控站点列表
     * @param payload
     * @return
     */
    @ApiOperation(value="areaCode查询监控站点列表", notes="areaCode查询监控站点列表")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "areCode", value = "行政区划", required = true, dataType = "String",paramType="query")
    })
    @PostMapping(value = "paging/query")
    public ResultBean<String> find(@RequestBody Map<String, Object> payload){
        ResultBean result = DataValidator.validateMapData(payload, "pageIndex", "pageSize", "condition");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS) {
            int pageIndex = Integer.parseInt(payload.get("pageIndex").toString());
            int pageSize = Integer.parseInt(payload.get("pageSize").toString());
            Map<String, Object> condition = (Map<String, Object>) payload.get("condition");
            Page<SysMonitorSiteInfo> industryList = service.find(pageIndex, pageSize, condition);
            PageChunk<SysMonitorSiteInfo> pageChunk = new PageChunk<>();
            if(industryList != null){
                return result.boolAdd(pageChunk.pageChunk(industryList));
            }else {
                return result.error("未查到站点列表");
            }
        }
        return result;
    }








}
